﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace Nau.Airplain.Common
{
    [Serializable]
    public class Configuration
    {
        public string Name { get; set; }
        public List<Group> Groups { get; set; }

        public static Configuration Load(string fileName)
        {
            if (string.IsNullOrWhiteSpace(fileName))
                throw  new ArgumentException("filePath is empty!");

            if (!File.Exists(fileName))
                throw new FileNotFoundException("Cannot find file ", fileName);

            FileStream serializationStream = new FileStream(fileName, FileMode.Open);
            BinaryFormatter formatter = new BinaryFormatter();
            Configuration newConfig = (Configuration) formatter.Deserialize(serializationStream);
            return newConfig;
        }

        public void Save(string fileName)
        {
            if (string.IsNullOrWhiteSpace(fileName))
                throw new ArgumentException("filePath cannot be null or empty!");

            FileStream serializationStream = new FileStream(fileName, FileMode.Create);
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(serializationStream, this);
        }
    }
}
